package com.meichis.mcsappframework.utils;


import android.content.Context;
import android.content.SharedPreferences;

import com.meichis.mcsappframework.BaseApplication;
import com.meichis.mcsappframework.entity.SPCache;

import java.util.Calendar;


public class SharePreferenceUtil {
    private SharedPreferences sp;
    private static volatile SharePreferenceUtil instance;
    private SharedPreferences.Editor editor;

    private static final String PREFERENCESNAME = "MCSPreferences";

    /*
     * Key for Preferences
     */
    public static final String PREFERENCES_AUTHKEY = "AuthKey";
    public static final String PREFERENCES_REFRESHAUTHKEY = "RAK"; //AUTHKEY
    public static final String PREFERENCES_ISFIRSTLOGIN = "IsFirstLogin";
    public static final String PREFERENCES_WILLLOST = "WillLost";
    public static final String PREFERENCES_LASTACTIVETIME = "LastActiveTime";
    public static final String PREFERENCES_LASTREFRESHPDTTIME = "LastRefreshPDTTime";
    public static final String PREFERENCES_APPMENU = "mu";
    public static final String PREFERENCES_APPMODULE = "ml";
    public static final String PREFERENCES_WEBSITEURL = "ws";
    public static final String PREFERENCES_DOWNLOADURL = "DownLoadUrl";
    public static final String PREFERENCES_REFRESH = "Re";
    public static final String PREFERENCES_AUTOLOGIN = "AL";
    public static final String PREFERENCES_CUSTOMER = "CU";
    public static final String PREFERENCES_HTTPCUSTOMER = "HCU";
    public static final String PREFERENCES_USERINFO = "ui";
    public static final String PREFERENCES_CLEARCACHE = "cc";
    public static final String PREFERENCES_CLEARCACHEMAIN = "ccm";
    public static final String PREFERENCES_DEVICECODE = "dc";
    public static final String PREFERENCES_ORDER = "od";
    public static final String PREFERENCES_PUBLISH = "pl";
    public static final String PREFERENCES_SERVERVERISON = "ServerVerison";
    public static final String PREFERENCES_ISSHOWUPDATEDIALOG = "IsShowUPdateDialog"; //判断是否弹出过升级提示
    public static final String PREFERENCES_ISDOWNAPPING = "ISDownAPPIng";
    public static final String PREFERENCES_ISINITDATA = "ISInitData";
    public static final String PREFERENCES_CALLBACK = "CB";
    public static final String PREFERENCES_OFFICIALCITYV = "OCV";
    public static final String PREFERENCES_OFFLINE = "OL";
    public static final String PREFERENCES_KEEPPWD = "KP";
    public static final String PREFERENCES_SCHEDULE_TIME = "ScheduleTime";
    public static final String PREFERENCES_VSTLOCATION = "vstLocation";//拜访地址
    public static final String PREFERENCES_ASKCHANGE = "ACG";
    public static final String PREFERENCES_ROTATEADV = "RotateAdv";//广告
    public static final String PREFERENCES_LASTUPDATETIME = "LastUpdateTime";
    public static final String PREFERENCES_MEMORY_AMAPLOCATION = "memoryAMapLocation";
    public static final String PREFERENCES_VEHICLE = "vehicle";
    public static final String PREFERENCES_TDP = "tdp";
    public static final String PREFERENCES_CODEPRINT = "codeprint";
    public static final String PREFERENCES_LASTGETMSGTIME = "LastMsgTime";//上次GetMsg时间
    /**
     * 用户名
     */
    public static final String PREFERENCES_USERNAME = "N";
    /**
     * 密码
     */
    public static final String PREFERENCES_PASSWORD = "P";
    /**
     * 登录设备信息
     */
    public static final String ExtParams = "ExtParams";

    public static final String PREFERENCES_DEVICEGUID = "DeviceGUID";


    public static SharePreferenceUtil getInstance() {
        if (instance == null) {
            synchronized (SharePreferenceUtil.class) {
                instance = new SharePreferenceUtil(BaseApplication.getContext());
            }
        }
        return instance;
    }

    private SharePreferenceUtil(Context context) {
        sp = context.getSharedPreferences(PREFERENCESNAME, Context.MODE_PRIVATE);
        editor = sp.edit();
        editor.apply();
    }

    public void setStringValue(String Key, String Value) {
        String valueBase64 = Tools.Base64Object(Value);
        editor.putString(Key, valueBase64);
        editor.commit();
    }

    public void setString(String Key, String Value) {
        editor.putString(Key, Value);
        editor.commit();
    }

    public String getStringValue(String Key) {
        String value = (String) Tools.DeBase64Object(sp.getString(Key, ""));
        if (value == "[]" || value == null)
            value = "";
        return value;
    }

    public String getString(String Key) {
        String value = sp.getString(Key, "");
        if (value == "[]" || value == null)
            value = "";
        return value;
    }

    public void setIntValue(String Key, int Value) {
        editor.putInt(Key, Value);
        editor.commit();
    }

    public int getIntValue(String Key) {
        int value = sp.getInt(Key, 1);
        return value;
    }

    public int getIntValue(String Key, int defaultvalue) {
        int value = sp.getInt(Key, defaultvalue);
        return value;
    }

    public Object getObjectValue(String key) {
        return Tools.DeBase64Object(sp.getString(key, null));
    }

    public void setObjectValue(String Key, Object Value) {
        String valueBase64 = Tools.Base64Object(Value);
        editor.putString(Key, valueBase64);
        editor.commit();

    }

    public void setLongValue(String Key, long Value) {
        editor.putLong(Key, Value);
        editor.commit();
    }

    public long getLongValue(String Key) {
        long value = sp.getLong(Key, 0);
        return value;
    }

    public void setBooleanValue(String Key, Boolean Value) {
        editor.putBoolean(Key, Value);
        editor.commit();
    }

    public boolean getBooleanValue(String Key, boolean defaultValue) {
        boolean value = sp.getBoolean(Key, defaultValue);
        return value;
    }

    /**
     * 保存缓存值
     *
     * @param key
     * @param Value
     * @param LimitTime 有效时间（分）
     */
    public void setCacheValue(String key, Object Value, int LimitTime) {
        SPCache cache = new SPCache();
        cache.setCacheValue(Value);
        cache.setUsefulTime(DateUtil.addMinute(Calendar.getInstance().getTime(), LimitTime));
        editor.putString(key, Tools.Base64Object(cache));
        editor.commit();
    }

    /**
     * 保存缓存值
     *
     * @param key
     * @param Value
     * @param effectiveDate 有效日期,日期格式 yyyy-MM-dd HH:mm:ss
     */
    public void setCacheValue(String key, Object Value, String effectiveDate) {
        SPCache cache = new SPCache();
        cache.setCacheValue(Value);
        cache.setUsefulTime(DateUtil.parse(effectiveDate));
        editor.putString(key, Tools.Base64Object(cache));
        editor.commit();
    }

    /**
     * 获取缓存值
     *
     * @param key
     * @return 如果超过了有效期则返回null
     */
    public Object getCacheValue(String key) {
        SPCache cache = (SPCache) getObjectValue(key);
        if (cache == null || Calendar.getInstance().getTime().compareTo(cache.getUsefulTime()) > 0) {
            return null;
        }
        return cache.getCacheValue();
    }

    public void remove(String key) {
        try {
            editor.remove(key);
            editor.commit();
        } catch (Exception e) {
            // TODO: handle exception
        }

    }

    public void clear() {
        editor.clear();
        editor.commit();
    }

}
